package com.stormrage.edu.dao.edu.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.stormrage.edu.dao.edu.EduCourseDao;
import com.stormrage.entity.edu.EduCourse;
import com.stormrage.entity.edu.rep.EduCourseRep;
import com.stormrage.entity.exam.rep.ExmExamCountRep;
import com.stormragetech.core.dao.MysBaseDao;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @Title: EduCourseDaoImpl.java
 * @Package com.stormrage.edu.dao.edu.impl
 * @Description: 课程 Dao
 * @author www.stormragetech.com
 * @date 2016年4月8日
 * @version V1.0
 */
@Repository
public class EduCourseDaoImpl extends MysBaseDao implements EduCourseDao {

	private static final Logger logger = LoggerFactory.getLogger(EduCourseDaoImpl.class);

	@Override
	public Integer create(final EduCourse arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduMajorDao 】-【 Create 】- 开始运行 ");

		final String query = "INSERT INTO edu_course (course, detail, image, lec_id, chap) VALUES"
				+ " (:course, :detail, :image, :lecId, :chap)";
		logger.debug(" 数据库执行语句为 " + query);

		Integer index = create(query, arg0);

		logger.debug(" 【 Dao 】-【 EduMajorDao 】-【 Create 】- 完成运行 ");
		return index;
	}

	@Override
	public void update(final EduCourse arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduMajorDao 】-【 Update 】- 开始运行 ");

		final String query = "UPDATE edu_course SET course = :course, detail = :detail, image = :image,"
				+ " lec_id = :lecId WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, arg0);

		logger.debug(" 【 Dao 】-【 EduMajorDao 】-【 Update 】- 完成运行 ");
	}

	@Override
	public void delete(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduMajorDao 】- 【 Delete 】- 开始运行 ");

		final String query = "DELETE FROM edu_course WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, map);

		logger.debug(" 【 Dao 】- 【 EduMajorDao 】- 【 Delete 】- 完成运行 ");
	}

	@Override
	public Integer queryCount(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduMajorDao 】- 【 QueryCount 】- 开始运行 ");

		final String query = "SELECT COUNT(*) FROM edu_course " + map.get("query");
		logger.debug(" 数据库执行语句为 " + query);

		Integer count = queryCount(query, map);

		logger.debug(" 【 Dao 】- 【 EduMajorDao 】- 【 QueryCount 】- 完成运行 ");
		return count;
	}

	@Override
	public EduCourse queryById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduMajorDao 】- 【 QueryById 】- 开始运行 ");

		final String query = "SELECT * FROM edu_course WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		EduCourse entity = queryObject(query, EduCourse.class, map);

		logger.debug(" 【 Dao 】- 【 EduMajorDao 】- 【 QueryById 】- 完成运行 ");
		return entity;
	}

	@Override
	public List<EduCourse> queryList() throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduCourseDao 】- 【 QueryList 】- 开始运行 ");

		final String query = "SELECT * FROM edu_course";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduCourse> array = queryList(query, EduCourse.class);

		logger.debug(" 【 Dao 】- 【 EduCourseDao 】- 【 QueryList 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduCourseRep> queryList(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduCourseDao 】- 【 QueryList 】- 开始运行 ");

		final String query = "SELECT a.id, a.course, a.chap, b.lec FROM edu_course AS a, edu_lec AS b WHERE a.lec_id = b.id "
				+ map.get("query") + " LIMIT :startPage, :size";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduCourseRep> array = queryList(query, EduCourseRep.class, map);

		logger.debug(" 【 Dao 】- 【 EduCourseDao 】- 【 QueryList 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduCourse> queryListById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduCourseDao 】- 【 QueryListById 】- 开始运行 ");

		final String query = "SELECT a.* FROM edu_course AS a, edu_module_course AS b"
				+ " WHERE a.id = b.course_id AND b.module_id = :id ORDER BY a.id";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduCourse> array = queryList(query, EduCourse.class, map);

		logger.debug(" 【 Dao 】- 【 EduCourseDao 】- 【 QueryListById 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduCourse> queryListByMajorId(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduCourseDao 】- 【 QueryListByMajorId 】- 开始运行 ");

		final String query = "SELECT a.* FROM edu_course AS a, edu_module_course AS b, edu_major_module AS c"
				+ " WHERE a.id = b.course_id AND b.module_id = c.module_id AND c.major_id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduCourse> array = queryList(query, EduCourse.class, map);

		logger.debug(" 【 Dao 】- 【 EduCourseDao 】- 【 QueryListByMajorId 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduCourse> queryRepListByTerm(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduCourseDao 】- 【 queryListByTermNess 】- 开始运行 ");

		final String query = "SELECT a.id, a.course FROM edu_course AS a,"
				+ " edu_module_course AS b, edu_major_module AS c, edu_module AS d, lea_term AS f"
				+ " WHERE a.id = b.course_id AND b.module_id = c.module_id AND b.module_id = d.id"
				+ " AND f.major_id = c.major_id AND f.id = :id AND d.ness = :ness";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduCourse> array = queryList(query, EduCourse.class, map);

		logger.debug(" 【 Dao 】- 【 EduCourseDao 】- 【 queryListByTermNess 】- 完成运行 ");
		return array;
	}

	@Override
	public List<ExmExamCountRep> queryRepListByTermNess(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduCourseDao 】- 【 queryListByTermNess 】- 开始运行 ");

		final String query = "SELECT a.id, a.course AS name, e.sele, e.mult, e.corr FROM edu_course AS a,"
				+ " edu_module_course AS b, edu_major_module AS c, edu_module AS d, exm_relate_count AS e, lea_term AS f"
				+ " WHERE a.id = b.course_id AND b.module_id = c.module_id AND b.module_id = d.id AND e.relate_id = a.id"
				+ " AND f.major_id = c.major_id AND e.type = 0 AND f.id = :id AND d.ness = :ness";
		logger.debug(" 数据库执行语句为 " + query);

		List<ExmExamCountRep> array = queryList(query, ExmExamCountRep.class, map);

		logger.debug(" 【 Dao 】- 【 EduCourseDao 】- 【 queryListByTermNess 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduCourseRep> queryRepListById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduCourseDao 】- 【 QueryRepListById 】- 开始运行 ");

		final String query = "SELECT a.course, a.chap, b.id, c.lec, b.id AS viewId FROM edu_course AS a, edu_module_course AS b,"
				+ " edu_lec AS c WHERE a.id = b.course_id AND b.module_id = :id AND a.lec_id = c.id ORDER BY a.id";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduCourseRep> array = queryList(query, EduCourseRep.class, map);

		logger.debug(" 【 Dao 】- 【 EduCourseDao 】- 【 QueryRepListById 】- 完成运行 ");
		return array;
	}

}
